/*
	Copyright 2009 Pavel Vakeš <pavel.vakes@gmail.com>
    Copyright 2007 Robert Knight <robertknight@gmail.com>
    Copyright 2007 Kevin Ottens <ervin@kde.org>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

// Own
#include "itemdelegate.h"
#include "messages_dialog_item.h"

// Qt
#include <QApplication>
#include <QFontMetrics>
#include <QIcon>
#include <QModelIndex>
#include <QPainter>
#include <QStyle>
#include <QStyleOptionViewItem>

// KDE
#include <KColorUtils>
#include <KColorScheme>
#include <KIcon>
#include <KDebug>
#include <KGlobal>
#include <KGlobalSettings>

// plasma
#include <Plasma/Plasma>


ItemDelegate::ItemDelegate(QObject *parent)
        : Plasma::Delegate(parent)
{
}

void ItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
    Plasma::Delegate::paint(painter, option, index);

    painter->save();

//    const bool hover = option.state & (QStyle::State_MouseOver | QStyle::State_Selected);
    const bool checkNowHover = (index.data(MessagesDialogItem::CheckNowHoverRole).value<bool>() == true);

	QRect timeRect = rectAfterTitle(option, index);

	timeRect.setRight(timeRect.right() - CHECK_NOW_WIDTH - CHECK_NOW_SPACING);
	QFont timeFont = option.font;
	timeFont.setPointSize(qMax(timeFont.pointSize() - 2, KGlobalSettings::smallestReadableFont().pointSize()));
	timeFont.setItalic(true);
	painter->setFont(timeFont);

	QRect iconRect = rectAfterTitle(option, index);
	iconRect.setLeft(iconRect.right() - CHECK_NOW_WIDTH);
	KIcon icon("view-refresh");

	int time_to_check = index.data(MessagesDialogItem::TimeToCheckRole).value<int>();

    if (checkNowHover && time_to_check > 0) {
		painter->setPen(QPen(KColorScheme(QPalette::Active).foreground(KColorScheme::NormalText), 1));
		painter->drawText(timeRect, Qt::AlignRight, "check now");
		icon.paint(painter, iconRect, Qt::AlignVCenter | Qt::AlignRight, KIcon::Active);
    }
    else {
		painter->setPen(QPen(KColorScheme(QPalette::Active).foreground(KColorScheme::InactiveText), 1));

		if (time_to_check > 0) {
			painter->drawText(timeRect, Qt::AlignRight, "next check in " + QString::number(time_to_check) + " minute" + (time_to_check > 1 ? "s" : ""));
			icon.paint(painter, iconRect, Qt::AlignVCenter | Qt::AlignRight, KIcon::Normal);
		}
		else {
			painter->drawText(timeRect, Qt::AlignRight, "check in progress");
			icon.paint(painter, iconRect, Qt::AlignVCenter | Qt::AlignRight, KIcon::Disabled);
		}
    }



	painter->setPen(Qt::NoPen);
	const QColor gradientColor = KColorScheme(QPalette::Active).background(KColorScheme::NormalBackground).color();

    QLinearGradient gr;
	QRect gradientRect(timeRect);
	gradientRect.setRight(gradientRect.left() + 25);

	gr.setStart(gradientRect.topRight());
	gr.setFinalStop(gradientRect.topLeft());
	gr.setColorAt(0.0, Qt::transparent);
	gr.setColorAt(1, gradientColor);
	painter->setBrush(QBrush(gr));
	painter->drawRect(gradientRect);

    painter->restore();
}

bool ItemDelegate::isVisible(const QModelIndex& index) const
{
    Q_ASSERT(index.isValid());

    return true;
}

